﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Yu.Cms.Common;
using Yu.Cms.IDAL;

namespace Yu.Cms.SQLServerDAL
{
    public  class Admin : IAdmin
    {
        private readonly string TableName = "Y_Admin";
        #region IAdmin 成员

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Yu.Cms.Model.Admin model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + TableName + " set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Password=@Password,");
            strSql.Append("FlDate=@FlDate,");
            strSql.Append("LlDate=@LlDate,");
            strSql.Append("Fip=@Fip,");
            strSql.Append("Lip=@Lip");
            strSql.Append(" where Uid=@Uid ");
            SqlParameter[] parameters = {
					new SqlParameter("@Uid", SqlDbType.Int,4),
					new SqlParameter("@UserName", SqlDbType.NVarChar,25),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@FlDate", SqlDbType.SmallDateTime),
					new SqlParameter("@LlDate", SqlDbType.SmallDateTime),
					new SqlParameter("@Fip", SqlDbType.VarChar,15),
					new SqlParameter("@Lip", SqlDbType.VarChar,15)};
            parameters[0].Value = model.Uid;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.FlDate;
            parameters[4].Value = model.LlDate;
            parameters[5].Value = model.Fip;
            parameters[6].Value = model.Lip;

            return DbHelperMsSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 返回实体类
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public Yu.Cms.Model.Admin GetModel(string UserName)
        {
            QueryParam qp = new QueryParam();
            qp.Where = " Where UserName='" + UserName + "'";
            Yu.Cms.Model.Admin fam = new Yu.Cms.Model.Admin();
            int RecordCount = 0;
            IList<Yu.Cms.Model.Admin> lst = this.GetList(qp, out RecordCount);
            if (lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        #endregion

        /// <summary>
        /// 将记录集转为Admin实体类 (Y_Admin)
        /// </summary>
        /// <param name="dr">记录集</param>
        /// <param name="Fileds">字段名列表</param>
        /// <returns>Admin</returns>
        protected Yu.Cms.Model.Admin Populate_Admin(IDataReader dr, Dictionary<string, string> Fileds)
        {
            Yu.Cms.Model.Admin nc = new Yu.Cms.Model.Admin();
            if (Fileds.ContainsKey("Uid") && !Convert.IsDBNull(dr["Uid"])) nc.Uid = Convert.ToInt32(dr["Uid"]); // 自动ID
            if (Fileds.ContainsKey("UserName") && !Convert.IsDBNull(dr["UserName"])) nc.UserName = Convert.ToString(dr["UserName"]).Trim(); // 用户昵称
            if (Fileds.ContainsKey("Password") && !Convert.IsDBNull(dr["Password"])) nc.Password = Convert.ToString(dr["Password"]).Trim(); // 用户密码（加密）
            if (Fileds.ContainsKey("FlDate") && !Convert.IsDBNull(dr["FlDate"])) nc.FlDate = Convert.ToDateTime(dr["FlDate"]); // 本次登录时间
            if (Fileds.ContainsKey("LlDate") && !Convert.IsDBNull(dr["LlDate"])) nc.LlDate = Convert.ToDateTime(dr["LlDate"]); // 上次登录时间
            if (Fileds.ContainsKey("Fip") && !Convert.IsDBNull(dr["Fip"])) nc.Fip = Convert.ToString(dr["Fip"]).Trim(); // 本次登录IP
            if (Fileds.ContainsKey("Lip") && !Convert.IsDBNull(dr["Lip"])) nc.Lip = Convert.ToString(dr["Lip"]).Trim(); // 上次登录IP
            return nc;
        }

        /// <summary>
        /// 返回Admin实体类的IList对象 (Y_Admin)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>Admin实体类的IList对象(Y_Admin)</returns>
        protected  IList<Yu.Cms.Model.Admin> GetList(QueryParam qp, out int RecordCount)
        {
            qp.TableName = TableName;
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "Uid";
            }
            else if (qp.Orderfld != "Uid")
            {
                qp.Orderfld += ",Uid";
            }

            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            else
            {
                qp.ReturnFields += ",";
                qp.ReturnFields += qp.Orderfld;
            }
            DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.Admin> mypd = new DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.Admin>(this.Populate_Admin);
            return DbHelperMsSQL.GetRecordListBySql(mypd, qp, out RecordCount);
        }
    }
}
